//
//  WJNetworkManager+Extention.swift
//  WJWEIBO
//
//  Created by 文杰 许                       on 2018/9/17.
//  Copyright © 2018年 文杰 许                      . All rights reserved.
//

import Foundation

extension WJNetwrokManager
{
    
    /// 首页微博数据
    ///
    /// - Parameters:
    ///   - since_id: 返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
    ///   - max_id: 返回ID小于或等于max_id的微博，默认为0。
    ///   - completion: 请求回调
    func statusList(since_id: Int64 = 0, max_id: Int64 = 0, completion: @escaping (_ list: [[String: AnyObject]]?, _ isSuccess: Bool)->())
    {
        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let param = ["since_id": since_id, "max_id":max_id]
        
        
        requestAccessToken(url: url, parameters: param) { (response) in
            
            if(response.error == nil)
            {
                let json = response.result.value as AnyObject
//                print("请求成功\(json)")
                let result = json["statuses"]
                completion(result as? [[String : AnyObject]], true)
            }
            else
            {
                if response.response?.statusCode == 403
                {
                    print("token过期，重新登录")
                }
                print("请求失败\(String(describing: response.error))")
                completion(nil, false)
            }
        }
    }
    
    func unReadCount(completion: @escaping (_ count: Int)->())
    {
        guard let uid = uid else { return }
        
        let url = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let param = ["uid": uid]
        
        requestAccessToken(url: url, parameters: param) { (response) in
            
            if(response.error == nil)
            {
                let json = response.result.value as AnyObject
                
                let count = json["status_24unread"] as? Int
                
                completion(count ?? 0)
            }
            else
            {
                if response.response?.statusCode == 403
                {
                    print("token过期，重新登录")
                }
                print("请求失败\(String(describing: response.error))")
            }
        }
    }
}
